package com.zmx.patientend.view.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;

import com.zmx.patientend.BaseActivity;
import com.zmx.patientend.R;
import com.zmx.patientend.okhttp.OkHttp3ClientManager;
import com.zmx.patientend.utils.AllUtil;
import com.zmx.patientend.view.util.MySharedPreferences;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TopUpActivity extends BaseActivity {

    @BindView(R.id.money)
    EditText money;
    @BindView(R.id.zfb_image)
    ImageView zfbImage;
    @BindView(R.id.zfb_layout)
    RelativeLayout zfbLayout;
    @BindView(R.id.wx_image)
    ImageView wxImage;
    @BindView(R.id.wx_layout)
    RelativeLayout wxLayout;
    @BindView(R.id.submit)
    Button submit;

    private boolean zf_state = false;//支付状态
    private Integer zf_type = 0;//支付方式

    @Override
    protected int getLayoutId() {
        return R.layout.activity_top_up;
    }

    @Override
    protected void initViews() {

        setToolbar(R.id.tool_bar);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.zfb_layout, R.id.wx_layout, R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.zfb_layout:

                zf_state = true;
                zf_type = 1;
                zfbImage.setVisibility(View.VISIBLE);
                wxImage.setVisibility(View.GONE);

                break;
            case R.id.wx_layout:

                zf_state = true;
                zf_type = 2;
                zfbImage.setVisibility(View.GONE);
                wxImage.setVisibility(View.VISIBLE);

                break;
            case R.id.submit:

                if (money.getText().toString().equals("")){
                    Toast("请输入充值金额");
                    return;
                }
                if (!zf_state){
                    Toast("选择充值方式");
                    return;
                }


                getBill();
                break;
        }
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:

                    Log.e("充值","充值"+msg.obj.toString());


                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());

                        Toast(object.getString("msg"));

                        if (object.getInt("code") == 0){
                            finish();
                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };


    public void getBill() {

        List<String> ls = new ArrayList<>();
        ls.add(money.getText().toString());
        ls.add(zf_type+"");
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "addMoney");
        params.put("data[money]", money.getText().toString());
        params.put("data[pay_type]", zf_type + "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/admin/api.member/entry", params, handler, 1, 404);


    }

}
